357B - Flag Day - CodeForces Solution


constructive algorithms implementation *1400

Please click on ads to support us..

Python Code:

n,m = map(int,input().split())

group = [list(map(int,input().split())) for _ in range(m)]

colours = [0]*(n+1)
visited = [False]*(n+1)

colours[group[0][0]]=1
colours[group[0][1]]=2
colours[group[0][2]]=3

visited[group[0][0]]=visited[group[0][1]]=visited[group[0][2]]=True

for i in range(1,m):
    curr = group[i]
    cset = set()
    for j in curr:
        if visited[j]:
            cset.add(colours[j])

    for j in curr:
        if visited[j]:
            continue
        
        if 1 not in cset:
            colours[j]=1
            cset.add(1)
        elif 2 not in cset:
            colours[j]=2
            cset.add(2)
        else:
            colours[j]=3
            cset.add(3)
        visited[j]=True

print(*colours[1:])

    


C++ Code:

#include <bits/stdc++.h>
using namespace std;
// typedef
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<pii> vpii;
typedef deque<int> di;
typedef deque<ll> dll;
// define instruction
#define double long double
#define rep(i, x, y) for (int i = x; i < y; i++)
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define all(x) begin(x), end(x)
// define constants
#define MOD 1000000007
#define inf 1e18
#define PI 3.141592653589793238462
// defined functions
ll setBitNumber(ll n)
{
	if (n == 0)
		return 0;
	ll msb = 0;
	n = n / 2;
	while (n != 0)
	{
		n = n / 2;
		msb++;
	}
	return (1 << msb);
}
ll countBits(ll number)
{ // log function in base  take only integer part
	return (ll)log2(number) + 1;
}
ll mul(ll a, ll b, ll mod = 1000000007)
{
	return ((a % mod) * (b % mod)) % mod;
}
ll gcd(ll a, ll b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}
bool isPrime(ll n)
{
	if (n <= 1)
		return false;
	if (n == 2 || n == 3)
		return true;
	if (n % 2 == 0 || n % 3 == 0)
		return false;

	for (ll i = 5; i <= sqrt(n); i = i + 6)
		if (n % i == 0 || n % (i + 2) == 0)
			return false;
	return true;
}
ll expo(ll a, ll b, ll mod)
{
	ll res = 1;
	while (b > 0)
	{
		if (b & 1)
			res = (res * a) % mod;
		a = (a * a) % mod;
		b = b >> 1;
	}
	return res;
}
int countDigit(ll n)
{
	return floor(log10(n) + 1);
}
ll lcm(ll a, ll b)
{
	return (a / gcd(a, b)) * b;
}
ll countDivisors(ll n)
{
	ll cnt = 0;
	for (ll i = 1; i <= sqrt(n); i++)
	{
		if (n % i == 0 && isPrime(i))
		{
			if (n / i == i)
				cnt++;
			else // Otherwise count both
				cnt = cnt + 2;
		}
	}
	return cnt;
}

int fact(int n);

int nCr(int n, int r)
{
	return fact(n) / (fact(r) * fact(n - r));
}

// Returns factorial of n
int fact(int n)
{
	if (n == 0)
		return 1;
	int res = 1;
	for (int i = 2; i <= n; i++)
		res = res * i;
	return res;
}

// main solution
//@aniketrajput25
void solve()
{
	int n, m;
	cin >> n >> m;
	map<int, int> mp;
	int a, b, c;
	cin >> a >> b >> c;
	mp[a] = 1;
	mp[b] = 2;
	mp[c] = 3;
	for (int i = 0; i < m - 1; i++)
	{
		vi arr(3);
		for (int j = 0; j < 3; j++)
			cin >> arr[j];
		bool f1 = true, f2 = true, f3 = true;

		for (auto x : arr)
		{
			if (mp.count(x) == 1)
			{
				if (mp[x] == 1)
				{
					f1 = false;
				}
				else if (mp[x] == 2)
					f2 = false;
				else if (mp[x] == 3)
					f3 = false;
			}
		}
		for (auto x : arr)
		{
			if (mp.count(x) == 0)
			{
				if (f1)
				{
					mp[x] = 1;
					f1 = false;
				}
				else if (f2)
				{
					mp[x] = 2;
					f2 = false;
				}
				else if (f3)
				{
					mp[x] = 3;
					f3 = false;
				}
			}
		}
	}
	for (int i=1;i<=n;i++)
		cout << mp[i] << " ";
}
int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}

	return 0;
}
	     			 			   		   	   	  	 	


Comments

Submit
0 Comments
More Questions

1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree